package com.tyk.bookstore.back.product.manager.excel.config;

import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.exception.CustomException;
import com.tyk.bookstore.back.common.core.model.result.CommonResultEnum;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.product.manager.excel.model.AuthorExcelVo;
import com.tyk.bookstore.back.product.manager.excel.model.BookExcelVo;
import com.tyk.bookstore.back.product.manager.excel.model.PublisherExcelVo;
import com.tyk.bookstore.back.product.model.entity.AuthorDo;
import com.tyk.bookstore.back.product.model.entity.BookDo;
import com.tyk.bookstore.back.product.model.entity.ClazzDo;
import com.tyk.bookstore.back.product.model.entity.PublisherDo;
import com.tyk.bookstore.back.product.model.result.AuthorResultEnum;
import com.tyk.bookstore.back.product.model.result.BookResultEnum;
import com.tyk.bookstore.back.product.model.result.ClazzResultEnum;
import com.tyk.bookstore.back.product.model.result.PublisherResultEnum;
import com.tyk.bookstore.back.product.repository.AuthorRepository;
import com.tyk.bookstore.back.product.repository.BookRepository;
import com.tyk.bookstore.back.product.repository.ClazzRepository;
import com.tyk.bookstore.back.product.repository.PublisherRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author tyk
 * @description BookExcelConfig
 */
@Configuration
@RequiredArgsConstructor
public class BookExcelConfig {

    private final BookRepository bookRepository;
    private final AuthorRepository authorRepository;
    private final PublisherRepository publisherRepository;
    private final ClazzRepository clazzRepository;

    @Bean
    public ExcelManager<BookExcelVo> bookExcelManager() {
        return new ExcelManager<>() {
            @Override
            public void checkRow(BookExcelVo data) {
                // ISBN不重复
                AssertUtil.isFalse(
                        bookRepository.exist1Eq(
                                BookDo::getIsbn, data.getIsbn(), BookDo.class
                        ),
                        BookResultEnum.PART_BOOK_ALREADY_EXISTS
                );
                // 作者存在
                AssertUtil.isTrue(
                        authorRepository.exist1Eq(AuthorDo::getId, data.getAuthorId(), AuthorDo.class),
                        AuthorResultEnum.AUTHOR_NOT_FOUND
                );

                // 出版社存在
                AssertUtil.isTrue(
                        publisherRepository.exist1Eq(PublisherDo::getId, data.getPublisherId(), PublisherDo.class),
                        PublisherResultEnum.PUBLISHER_NOT_FOUND
                );

                // 分类存在
                AssertUtil.isTrue(
                        clazzRepository.exist1Eq(ClazzDo::getId, data.getClazzId(), ClazzDo.class),
                        ClazzResultEnum.CLAZZ_NOT_FOUND
                );
                AssertUtil.isFalse(
                        clazzRepository.exist1Eq(ClazzDo::getParentId, data.getClazzId(), ClazzDo.class),
                        ClazzResultEnum.CLAZZ_HAS_CHILD
                );
            }

            @Override
            public void handleSaveException(MultipartFile file, Exception e) {
                throw new CustomException(CommonResultEnum.FILE_OR_DATA_FORMAT_ERROR);
            }
        };
    }

    @Bean
    public ExcelManager<AuthorExcelVo> authorExcelManager() {
        return new ExcelManager<>() {
            @Override
            public void checkRow(AuthorExcelVo data) {
                // 作者不重复
                AssertUtil.isFalse(
                        authorRepository.exist2Eq(
                                AuthorDo::getName, data.getName(),
                                AuthorDo::getDescription, data.getDescription(),
                                AuthorDo.class
                        ),
                        AuthorResultEnum.PART_AUTHOR_ALREADY_EXISTS
                );
            }

            @Override
            public void handleSaveException(MultipartFile file, Exception e) {
                throw new CustomException(CommonResultEnum.FILE_OR_DATA_FORMAT_ERROR);
            }
        };
    }

    @Bean
    public ExcelManager<PublisherExcelVo> publisherExcelManager() {
        return new ExcelManager<>() {
            @Override
            public void checkRow(PublisherExcelVo data) {
                // 出版社不重复
                AssertUtil.isFalse(
                        publisherRepository.exist1Eq(
                                PublisherDo::getName, data.getName(), PublisherDo.class
                        ),
                        PublisherResultEnum.PART_PUBLISHER_ALREADY_EXISTS
                );
            }

            @Override
            public void handleSaveException(MultipartFile file, Exception e) {
                throw new CustomException(CommonResultEnum.FILE_OR_DATA_FORMAT_ERROR);
            }
        };
    }
}
